/**
 * @author Renato Lui Geh
 * @author Yan Soares Couto
 * 
 * @version 0.0.1
 * 
 * 12/06/2011
 * 
 */

package zombies;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Player {
    private final int UP = 1;
    private final int DOWN = 2;
    private final int RIGHT = 3;
    private final int LEFT = 4;
    
    private int prevDirection;
    
    private final int panelWidth = Zombies.WIDTH;
    private final int panelHeight = Zombies.HEIGHT;
    private ImageIcon playerIcon;
    private String player = "downDefault.gif";
    private Image playerImage;
    private int width, height;
    private int pos_x, pos_y;
    private int dx, dy;
    
    public Player(int pos_x, int pos_y) {
        playerIcon = new ImageIcon(this.getClass().getResource(player));
        playerImage = playerIcon.getImage();
        
        width = playerImage.getWidth(null);
        height = playerImage.getHeight(null);
        
        this.pos_x = pos_x;
        this.pos_y = pos_y;
    }
    public Player() {
        playerIcon = new ImageIcon(this.getClass().getResource(player));
        playerImage = playerIcon.getImage();
        
        width = playerImage.getWidth(null);
        height = playerImage.getHeight(null);
        
        pos_x = 250;
        pos_y = 250;
    }
    public void move() {
        pos_x += dx;
        pos_y += dy;
        
        if(pos_x < 1 || pos_x > panelWidth - 35)
            dx = -dx;
        
        if(pos_y < 1 || pos_y > panelHeight -50)
            dy = -dy;
    }
    public Rectangle getBounds() {
        return new Rectangle(pos_x, pos_y, width, height);
    }
    public void keyPressed(KeyEvent event) {
        int key = event.getKeyCode();
        
        if(key == KeyEvent.VK_UP) {
            dy = -1;
            playerIcon = new ImageIcon(this.getClass().getResource(moveUp));
            playerImage = playerIcon.getImage();
            prevDirection = UP;
        }
        if(key == KeyEvent.VK_DOWN) {
            dy = 1;
            playerIcon = new ImageIcon(this.getClass().getResource(moveDown));
            playerImage = playerIcon.getImage();
            prevDirection = DOWN;
        }
        if(key == KeyEvent.VK_LEFT) {
            dx = -1;
            playerIcon = new ImageIcon(this.getClass().getResource(moveLeft));
            playerImage = playerIcon.getImage();
            prevDirection = LEFT;
        }
        if(key == KeyEvent.VK_RIGHT) {
            dx = 1;
            playerIcon = new ImageIcon(this.getClass().getResource(moveRight));
            playerImage = playerIcon.getImage();
            prevDirection = RIGHT;
        }
        if(key == KeyEvent.VK_SPACE) {
            dx = 0;
            dy = 0;
            
            if(prevDirection == UP) {
                    playerIcon = new ImageIcon(this.getClass().getResource(swordUp));
                    playerImage = playerIcon.getImage();
            }
            if(prevDirection == DOWN) {
                    playerIcon = new ImageIcon(this.getClass().getResource(swordDown));
                    playerImage = playerIcon.getImage();
            }
            if(prevDirection == RIGHT) {
                    playerIcon = new ImageIcon(this.getClass().getResource(swordRight));
                    playerImage = playerIcon.getImage();
            }
            if(prevDirection == LEFT) {
                    playerIcon = new ImageIcon(this.getClass().getResource(swordLeft));
                    playerImage = playerIcon.getImage();
            }
        }
        if(key == KeyEvent.VK_B) {
            dx = 0;
            dy = 0;
            
            if(prevDirection == UP) {
                    playerIcon = new ImageIcon(this.getClass().getResource(shieldUp));
                    playerImage = playerIcon.getImage();
            }
            if(prevDirection == DOWN) {
                    playerIcon = new ImageIcon(this.getClass().getResource(shieldDown));
                    playerImage = playerIcon.getImage();
            }
            if(prevDirection == RIGHT) {
                    playerIcon = new ImageIcon(this.getClass().getResource(shieldRight));
                    playerImage = playerIcon.getImage();
            }
            if(prevDirection == LEFT) {
                    playerIcon = new ImageIcon(this.getClass().getResource(shieldLeft));
                    playerImage = playerIcon.getImage();
            }
        }
    }
    public void keyReleased(KeyEvent event) {
        dx = 0;
        dy = 0;
        
        if(prevDirection == UP) {
                playerIcon = new ImageIcon(this.getClass().getResource(stillUp));
                playerImage = playerIcon.getImage();
        }
        if(prevDirection == DOWN) {
                playerIcon = new ImageIcon(this.getClass().getResource(stillDown));
                playerImage = playerIcon.getImage();
        }
        if(prevDirection == RIGHT) {
                playerIcon = new ImageIcon(this.getClass().getResource(stillRight));
                playerImage = playerIcon.getImage();
        }
        if(prevDirection == LEFT) {
                playerIcon = new ImageIcon(this.getClass().getResource(stillLeft));
                playerImage = playerIcon.getImage();
        }
    }
    public Image getImage() {
        return playerImage;
    }
    public int getX() {
        return pos_x;
    }
    public int getY() {
        return pos_y;
    }
    
    private String moveUp = "up.gif";
    private String moveDown = "down.gif";
    private String moveRight = "right.gif";
    private String moveLeft = "left.gif";
    
    private String stillUp = "upDefault.gif";
    private String stillDown = "downDefault.gif";
    private String stillLeft = "leftDefault.gif";
    private String stillRight = "rightDefault.gif";
    
    private String swordUp = "swordUp.gif";
    private String swordDown = "swordDown.gif";
    private String swordRight = "swordRight.gif";
    private String swordLeft = "swordLeft.gif";
    
    private String shieldUp = "shieldUp.gif";
    private String shieldDown = "shieldDown.gif";
    private String shieldRight = "shieldRight.gif";
    private String shieldLeft = "shieldLeft.gif";
}